class itemInfo
{
	public var itemType : int;
	public var itemIcon : Texture;
	public var itemAmount : int;
	public var workTime : float;
	public var workScript;
	
	public var inUse : boolean = true;
	public var elapsedTime : float;
	public var barOpacity : float;
	
	public var iconPos : Vector2;
	public var iconSize : Vector2;
	public var special : boolean;
	
	public function setData(i : pickUpInfo, c : GameObject)
	{
		itemType = i.itemType;
		itemIcon = i.itemIcon;
		itemAmount = i.itemAmount;
		workTime = i.workTime;
		workScript = c.GetComponent(i.workScript);
	}
}

var itemSlot : itemInfo[];
var labelSize : Vector2;
var labelPos : Vector2;

function collect(i : pickUpInfo, c : GameObject) : boolean
{
	var slot : int = findSlot(i.itemType);
	if (slot>=0)
	{
		if (itemSlot[slot].itemIcon==null)
		{
			itemSlot[slot].setData(i, c);
		}
		else
		{
			itemSlot[slot].itemAmount+=i.itemAmount;
		}
		return true;
	}
	else
	{
		return false;
	}
}

function findType(itemType : int) : int
{
	for (var i : int = 0; i < itemSlot.length; i++)
	{
		if (itemSlot[i].itemIcon!=null && itemSlot[i].itemType==itemType)
		{
			return i;
		}
	}
	return -1;
}

function findSlot(itemType : int) : int
{
	for (var i : int = 0; i < itemSlot.length; i++)
	{
		if (itemSlot[i].itemIcon==null || (itemSlot[i].itemIcon!=null && itemSlot[i].itemType==itemType))
		{
			return i;
		}
	}
	return -1;
}

function removeItem(slot : int, itemAmount : int) : int
{
	if (itemSlot[slot].itemAmount<itemAmount)
	{
		itemAmount=itemSlot[slot].itemAmount;
	}
	itemSlot[slot].itemAmount -=itemAmount;
	return itemAmount;
}

function deleteItem(slot : int, itemAmount : int) : int
{
	if (itemSlot[slot].itemAmount<itemAmount)
	{
		itemAmount=itemSlot[slot].itemAmount;
		itemSlot[slot].itemIcon = null;
	}
	itemSlot[slot].itemAmount -=itemAmount;
	return itemAmount;

}

function useItem (itemType : int) : int
{
	return useItem(itemType, 1);
}

function useItem (itemType : int, itemAmount : int) : int
{
	var slot : int = findType(itemType);
	if (slot>=0)
	{
		if (itemSlot[slot].inUse)
			return 0;
			
		if (itemSlot[slot].workScript!=null)
			itemSlot[slot].workScript.onStartUsing();
		
		if (itemSlot[slot].workTime>0)
		{
			itemSlot[slot].inUse = true;
			itemSlot[slot].elapsedTime = itemSlot[slot].workTime;
		}
		
		if (itemSlot[slot].special)
			return removeItem(slot, itemAmount);
		else
			return deleteItem(slot, itemAmount);
	}
	else
	{
		return 0;
	}
}
